///////////////////////////////////////////////////////////////////////////////////////////////////
// Illusion Ray Development Kit (IDK)           Copyright (c) 2010 ( dimitriy.martynenko@gmail.com )
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2010-05-22
// Updated : ...
// Licence : NEW BSD
// Info	   : IDK Engine
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once

namespace Core
{
	class TMeshLOD : public TResource, virtual public IDK::IRenderable
	{
		IDKClass;
		typedef TResource Base;
	protected:
		struct Type
		{
			enum Enum
			{
				Trinagle,
				Vertex,
				Max,
			};
		};

		struct VertexAttrib
		{
			Math::Vector3 position;
			Math::Vector3 normal;
			Math::Vector3 tangent;
			Math::Vector3 texcoord;
		};
		typedef DynamicArray<VertexAttrib> VertexData;

		struct TrinagleAttrib
		{
			uint16 p1, p2, p3;
		};
		typedef DynamicArray<TrinagleAttrib> TrinagleData;
	protected:
		TrinagleData mTrinagleData;
		VertexData mVertexData;
		uint16 mCount[Type::Max];
		//
		IDK::GPUIndexBuffer mIndexBuffer;
		IDK::GPUVertexBuffer mVertexBuffer;
	public:		
		TMeshLOD(Threading::IThreadEvent& signal, ref<IDK::IStream> stream);
	public://
		virtual bool OnTickLoad(void);			
		virtual void Cleanup(void);
		virtual void OnChangeLoadStatus(LoadStatus::Enum status);
	public:
		virtual void Rendered(IDK::IRenderState& rndState);
		virtual bool Render(IDK::IRenderState& rndState);
	private:
		void BindToGPU(IDK::IRenderState& rndState);
	};

	typedef ref<TMeshLOD> MeshLOD;

	class TMeshLODStreaming : public TMeshLOD
	{
		typedef TMeshLOD Base;
	private:
		struct LoadProcess
		{
			enum Enum
			{
				None,
				VersionAndName,
				InitIndeces,
				ReadIndeces,
				InitVertices,
				ReadVertices,
				Complete
			};
		};
	public:
		TMeshLODStreaming(Threading::IThreadEvent& signal, ref<IDK::IStream> stream);
		virtual bool OnTickLoad(void);
		virtual void Cleanup(void);
	private:
		//for streaming heleper info 
		uint mIndecesReadInBytes;
		uint mVerticesReadInBytes;
		LoadProcess::Enum mLoadProcess;
	};

	typedef ref<TMeshLODStreaming> MeshLODStreaming;

	class TMesh : public TArray<MeshLOD>, virtual public IDK::IMesh
	{
		IDKClass;
		typedef TArray<MeshLOD> Base;
	private:
		String mPath;
	public:
		TMesh(const String &path);
	public:
		String GetPath(void) const;
		void AddLOD(MeshLOD meshLOD);
		virtual void Rendered(IDK::IRenderState& rndState);
		virtual bool Render(IDK::IRenderState& rndState);
	};

	typedef ref<TMesh> Mesh;
}
